﻿using Candy.Infrastructure;
using Candy.Model;
using GalaSoft.MvvmLight.Command;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace Candy.ViewModel
{
    public class LoadProjectViewModel : CandyViewModelBase
    {
        IDataService _dataService;
        INavigationService _navigationService;

        public LoadProjectViewModel(IDataService dataService, INavigationService navigationService)
        {
            _dataService = dataService;
            _navigationService = navigationService;

        }


        private RelayCommand<ProjectEntityViewModel> _loadProject;
        public RelayCommand<ProjectEntityViewModel> LoadProject
        {
            get
            {
                return _loadProject ?? (_loadProject = new RelayCommand<ProjectEntityViewModel>(ExecLoadProject, CanLoadProject));
            }
        }

        /// <summary>
        /// Checks whether the LoadProject command is executable
        /// </summary>
        private bool CanLoadProject(ProjectEntityViewModel val)
        {
            return true;
        }

        /// <summary>
        /// Executes the LoadProject command 
        /// </summary>
        private void ExecLoadProject(ProjectEntityViewModel val)
        {
            _dataService.Initialize(val.Entity, op =>
                {
                    if (op.HasError)
                    {
                        base.HandleDomainError(op, "Unable to load project.");
                    }
                    else
                    {
                        _navigationService.Navigate(NavigationTargets.Project);
                    }
                });
        }


    }
}
